package com.x.reader.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.framework.model.BaseView;
import com.x.framework.model.ResData;
import com.x.reader.model.User;
import com.x.reader.service.UserService;
import com.x.security.model.Account;
import com.x.security.model.Resource;
import com.x.security.rpc.SecurityRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.x.framework.Base;
import com.x.security.model.SecurityAccount;
import com.x.security.util.Constant;

@Controller
@RestController
public class UserController extends Base {
    private static ModelAndView loginModelAndView = new ModelAndView("/home/index.jsp");
    private static ModelAndView loginWxModelAndView = new ModelAndView("/wx/framework.jsp");

    @Autowired
    private UserService userService;

    @Reference(interfaceName = "com.x.security.rpc.SecurityRpcService")
    private SecurityRpcService securityRpcService;

    @RequestMapping(value = "/GetUserJson")
    public ResData getUserJson(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("SECURITY_TOKEN====================" + response.getHeader(Constant.SECURITY_TOKEN));
        String accountId = request.getAttribute("accountId").toString();
        User user = new User();
        user.setAccountId(accountId);
        List<User> userList = userService.selectUserList(user);
        ResData resData = new ResData(0);
        if (userList.size() > 0) {
            user = userList.get(0);
            user.setAccountToken(response.getHeader(Constant.SECURITY_TOKEN));
            resData.setCode(1);
            resData.setResult(user);
        }
        return resData;
    }

    @RequestMapping(value = "/GetUserSession")
    public ModelAndView getUserSession(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        System.out.println("====================" + response.getHeader(Constant.SECURITY_TOKEN));
        SecurityAccount account = (SecurityAccount) session.getAttribute(Constant.SECURITY_ACCOUNT);
        String accountId = account.getAccountId();
        User user = new User();
        user.setAccountId(accountId);
        List<User> userList = userService.selectUserList(user);
        if (userList.size() > 0) {
            user = userList.get(0);
            session.setAttribute("userId", user.getUserId());
            session.setAttribute("userName", user.getUserName());
            session.setAttribute("userMobile", user.getUserMobile());
            session.setAttribute("userAddress", user.getUserAddress());
            session.setAttribute("userNickName", user.getUserNickName());
            session.setAttribute("userEmail", user.getUserEmail());
        }
        return loginModelAndView;
    }

    @RequestMapping(value = "/SelectUser")
    public ResData SelectUser(User user) {
        List<User> userList = userService.selectUserList(user);
        ResData resData = new ResData(0);
        if (userList.size() > 0) {
            user = userList.get(0);
            resData.setCode(1);
            resData.setResult(user);
        }
        return resData;
    }

    @RequestMapping(value = "/SelectAccountResourceList")
    public ResData selectAccountResourceList(Account account, String applicationId) {
        ResData resData = new ResData(0);
        List<Resource> resourceList = securityRpcService.selectAccountResourceList(account, applicationId);
        if (resourceList.size() > 0) {
            resData.setCode(1);
            resData.setResult(resourceList);
        }
        return resData;
    }

    @RequestMapping(value = "/UpdateUser")
    public BaseView updateUser(User user) {
        userService.updateUser(user);
        BaseView view = new BaseView(1);
        return view;
    }

    @RequestMapping(value = "/UpdateAccountPassword")
    public BaseView updateAccountPassword(Account account, String accountOldPassword) {
        int flag = 0;
        try {
            Account a = new Account();
            a.setAccountId(account.getAccountId());
            List<Account> accountList = securityRpcService.selectAccountList(a);
            if (accountList.size() > 0) {
                a = accountList.get(0);
                String accountPasswordEncryption = a.getAccountPassword();
                System.out.println(accountOldPassword);
                System.out.println(accountPasswordEncryption);
                System.out.println(securityRpcService.validateAccountPassword(accountOldPassword, accountPasswordEncryption));
                if (securityRpcService.validateAccountPassword(accountOldPassword, accountPasswordEncryption)) {
                    securityRpcService.updateAccount(account);
                    flag = 1;
                }
            }
//            securityRpcService.updateAccount(account);
//            flag = 1;
        } finally {
            BaseView view = new BaseView(flag);
            if (flag == 0) {
                view.setMsg("原密码错误");
            }
            return view;
        }

    }

}
